﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

public class BaseWinFrom : Form
{
    public BaseWinFrom()
    {
    }

    //public void AddControl(Control control)
    //{
    //    //this.MdiChildren   
    //}

    //public void AddTopControl(Control control)
    //{
    //    BaseWinFrom bwf = this.TopLevelControl as BaseWinFrom;
    //    if (bwf == null)
    //        return;
    //    bwf.AddControl(control);
    //}

    public void AddMdiChildren(BaseWinFrom Form)
    {
        if (Form == null)
            return;
        Form.MdiParent = this;
        Form.Show();
    }

    public void AddMdiParent(BaseWinFrom Form)
    {
        if (Form == null)
            return;
        Form.MdiParent = this.MdiParent;
        Form.Show();
    }
}

public class BaseLoginFrom : BaseWinFrom
{
    public BaseLoginFrom()
    {
        if (ApplictionData.CurrentUser == null)
        {
            this.Dispose(true);
        }
    }
}

public class TextBoxDataTimePicker : TextBox
{
    MonthCalendar mc = null;

    public TextBoxDataTimePicker()
    {
    }

    protected override void OnClick(EventArgs e)
    {
        base.OnClick(e);
        if (mc == null)
        {
            mc = new MonthCalendar();
            this.Parent.Controls.Add(mc);

            mc.MaxSelectionCount = 1;
            mc.ScrollChange = 120;


            mc.Top = this.Top + this.Height;
            if (this.Left + this.Width > mc.Width)
            {
                mc.Left = this.Left + this.Width - mc.Width;
            }
            else
            {
                mc.Left = this.Left;
            }
            mc.DateSelected += new DateRangeEventHandler(mc_DateSelected);
        }
        mc.Visible = true;
    }

    private void mc_DateSelected(object sender, DateRangeEventArgs e)
    {
        if (mc == null)
            return;
        this.Text = e.Start.ToString("yyyy-MM-dd:HH:mm:ss");
        mc.Visible = false;
        this.Focus();
    }
}

public class TextBoxBindMonthCalendar
{
    MonthCalendar mc = null;
    TextBox txtBox = null;
    public TextBoxBindMonthCalendar(TextBox bindTextBox)
    {
        txtBox = bindTextBox;
        if (bindTextBox != null)
        {
            bindTextBox.Click += new EventHandler(bindTextBox_Click);
        }
        if (mc == null)
        {
            mc = new MonthCalendar();
            AddControl(txtBox,mc);
            mc.MaxSelectionCount = 1;
            mc.TabIndex = 0;
            mc.ScrollChange = 120;


            mc.Top = txtBox.Top + txtBox.Height;
            if (txtBox.Left + txtBox.Width > mc.Width)
            {
                mc.Left = txtBox.Left + txtBox.Width - mc.Width;
            }
            else
            {
                mc.Left = txtBox.Left;
            }
            mc.DateSelected += new DateRangeEventHandler(mc_DateSelected);
            mc.Visible = false;
        }
    }

    void bindTextBox_Click(object sender, EventArgs e)
    {
        mc.Visible = true;
    }

    void AddControl(Control p, Control c)
    {
        if(p==null || c==null)
            return;
        if (p.Parent != null)
        {
            p = p.Parent;
        }
        p.Controls.Add(c);
        p.Controls.SetChildIndex(c, 0);
    }

    void mc_DateSelected(object sender, DateRangeEventArgs e)
    {
        if (mc == null)
            return;
        txtBox.Text = e.Start.ToString("yyyy-MM-dd HH:mm:ss");
        mc.Visible = false;
        txtBox.Focus();
    }
}
